package com.lenin.omena.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.lenin.omena.client.UserInfoForm.ActionType;
import com.lenin.omena.client.event.DeleteUserEvent;
import com.lenin.omena.client.event.DeleteUserEventHandler;
import com.lenin.omena.client.event.LoginEvent;
import com.lenin.omena.client.event.LoginEventHandler;
import com.lenin.omena.client.event.SaveUserEvent;
import com.lenin.omena.client.event.SaveUserEventHandler;
import com.lenin.omena.shared.DeleteUserResult;
import com.lenin.omena.shared.LoginResult;
import com.lenin.omena.shared.SaveUserResult;
import com.lenin.omena.shared.domain.Produce;
import com.lenin.omena.shared.domain.User;

public class DialogPopupPanel extends Composite 
	implements LoginEventHandler, SaveUserEventHandler, DeleteUserEventHandler {
	
	interface MyUiBinder extends UiBinder<Widget, DialogPopupPanel> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	
	public enum DialogView {
		LOGIN, REGISTER,
		MANAGE_PRODUCE, EDIT_INFO
	}
	
	@UiField
	SimplePanel contentPanel;

	@UiField
	TabLayoutPanel tabPanel;

	private LoginForm loginForm;
	private UserInfoForm registerForm;
	private ProduceManagerPanel produceManagerPanel;
	private UserInfoForm userInfoForm;
	
	private DialogView view;

	public DialogPopupPanel() {

		initWidget(uiBinder.createAndBindUi(this));

		LoginEvent.register(AppleMarket.getEventBus(), this);
		SaveUserEvent.register(AppleMarket.getEventBus(), this);
		DeleteUserEvent.register(AppleMarket.getEventBus(), this);

		tabPanel.setAnimationDuration(500);

		User user = AppleMarket.getUserContext().getUser();

		if (user == null) {

			loginForm = new LoginForm();
			registerForm = new UserInfoForm();
			tabPanel.add(loginForm, "Kirjaudu sisään");
			tabPanel.add(registerForm, "Rekisteröidy");
			
			view = DialogView.LOGIN;

		} else {

			produceManagerPanel = new ProduceManagerPanel();
			List<Produce> produce = user.getSite().getProduce();
			if (produce != null) {
				produceManagerPanel.updateProduceTable(produce);
			}
			
			userInfoForm = new UserInfoForm(user.getUserInfo(),
					ActionType.SAVE_USER_INFO);
			
			tabPanel.add(produceManagerPanel, "Aseta tarjolle");
			tabPanel.add(userInfoForm, "Käyttäjätiedot");
			
			view = DialogView.MANAGE_PRODUCE;
			
		}

		tabPanel.selectTab(0);
		
	}
	
	
	public DialogView getView() {
		
		return view;
		
	}
	

	@Override
	public void onLogin(LoginEvent event) {

		LoginResult result = event.getResult();

		if (result.getStatus() == 0) {

			if (event.getActionType() == LoginEvent.ActionType.LOGIN) {
				
				User user = result.getUser();
				
				tabPanel.remove(registerForm);
				tabPanel.remove(loginForm);

				if (tabPanel.getWidgetCount() == 0) {

					produceManagerPanel = new ProduceManagerPanel();
					List<Produce> produce = user.getSite().getProduce();
					if (produce != null) {
						produceManagerPanel.updateProduceTable(produce);
					}
					
					userInfoForm = new UserInfoForm(user.getUserInfo(),
							ActionType.SAVE_USER_INFO);

					tabPanel.add(produceManagerPanel, "Aseta tarjolle");
					tabPanel.add(userInfoForm, "Käyttäjätiedot");
					
					view = DialogView.MANAGE_PRODUCE;

				}

			} else {
				
				tabPanel.remove(produceManagerPanel);
				tabPanel.remove(userInfoForm);
				
				produceManagerPanel = null;
				userInfoForm = null;
				
				loginForm = new LoginForm();
				registerForm = new UserInfoForm();
				
				tabPanel.add(loginForm, "Kirjaudu sisään");
				tabPanel.add(registerForm, "Rekisteröidy");
				
				view = DialogView.LOGIN;
				
			}

		}
		
	}
	
	
	@Override
	public void onSaveUser(SaveUserEvent event) {
		
		SaveUserResult result = event.getResult();
		
		if(result.getStatus() == 0) {
			tabPanel.remove(registerForm);
		}
		
	}
	
	
	@Override
	public void onDeleteUser(DeleteUserEvent event) {
		
		DeleteUserResult result = event.getResult();
		
		if(result.getStatus() == 0) {
			AppleMarket.doLogout();
		}
		
	}

}
